<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ArrayBuild
 *
 * @author Tomek
 */
class ArrayBuild
{

	public $links = array();
	public $employees = array();
	public $jobs = array();
	public $dates = array();
	public $locations = array();
	public $jobsOffer = array();
	public $href = array();
	public $content = array();
	private $SiteName;
	private $searchLink;
	private $DOM;
	private $linksPath;
	private $hrefPath;
	private $empPath;
	private $jobPath;
	private $datePath;
	private $locPath;

	/**
	 *
	 * @param string $SiteName Strona z importu
	 * @param string $searchLink Strona do wyszukiwarki
	 * @param string $linksPath Ścieżka XPath do linków podstron
	 * @param string $hrefPath Ścieżka XPath do linkiów ogłoszeń
	 * @param string $empPath Ścieżka XPath do nazw pracodawców
	 * @param string $jobPath Ścieżka XPath do Pracy
	 * @param string $datePath Ścieżka XPath do daty
	 * @param string $locPath Ścieżka XPath do lokalizacji
	 * @param string $contentPath Ścieżka XPath do contentu ogłoszenia
	 */
	public function __construct($SiteName, $searchLink, $linksPath = '', $hrefPath = '', $empPath = '', $jobPath = '', $datePath = '', $locPath = '', $contentPath = '')
	{
		$this->searchLink = $searchLink;
		$this->SiteName = $SiteName;
		$this->linksPath = $linksPath;
		$this->hrefPath = $hrefPath;
		$this->empPath = $empPath;
		$this->jobPath = $jobPath;
		$this->datePath = $datePath;
		$this->locPath = $locPath;
	}

	public function getLinks()
	{
		$this->links[] = $this->searchLink;
		$html = new DOMDocument();
		$html->loadHTML(file_get_contents($this->SiteName . $this->links[0]));
		$this->DOM = new DOMXPath($html);

		$result = $this->DOM->query($this->linksPath);

		foreach ($result as $r) {
			$this->links[] = $r->value;
		}
		
	}

	public function getHref()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);

			$linkList = $this->DOM->query($this->hrefPath);
			foreach ($linkList as $hrefElement) {
				preg_replace('/[^a-zA-Z0-9_%\[().\]\\/-]/s', '',  $hrefElement->value);
				$this->href[] = $hrefElement->value;
			}
		}
		
	}

	public function getEmp()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);

			$empList = $this->DOM->query($this->empPath);
			foreach ($empList as $emp) {

				$this->employees[] = $emp->nodeValue;
			}
		}
	}

	public function getJobName()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);

			$jobList = $this->DOM->query($this->jobPath);

			foreach ($jobList as $job) {
				$this->jobs[] = $job->nodeValue;
			}
		}
	}

	public function getDates()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);

			$datesList = $this->DOM->query($this->datePath);

			foreach ($datesList as $date) {
				$this->dates[] = $date->nodeValue;
			}
		}
	}

	public function getLoc()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);

			$locationsList = $this->DOM->query($this->locPath);

			foreach ($locationsList as $elem) {

				$this->locations[] = $elem->nodeValue;
			}
		}
	}

	public function getContent()
	{
		foreach ($this->links as $l) {
			$html = new DOMDocument();
			$html->loadHTML(file_get_contents($this->SiteName . $l));
			$this->DOM = new DOMXPath($html);
			
			foreach ($this->href as $h) {
				$html = new DOMDocument();
				$html->loadHTML(file_get_contents($h));
				$XMLPath = new DOMXPath($html);
				$res = $XMLPath->query('//*[@id="Kolumne234"]');
				$output = '';
				foreach ($res as $r) {
					$output .= $r->nodeValue;
				}
				$this->content[] = substr($output,0, 200);
			}
		}
		
		
		

		
	}

	public function getElements()
	{
		$this->getLinks();
		$this->getDates();
		$this->getEmp();
		$this->getHref();
		$this->getJobName();
		$this->getLinks();
		$this->getLoc();
		$this->getContent();
	}

	public function generateXML()
	{
		require_once 'XMLGen.php';
		$XML = new XMLGen(date("d_m_Y_G_i_s") . '.xml', $this->employees, $this->jobs, $this->dates, $this->locations, $this->content, $this->href);
		$XML->generateXML();
	}

}

?>
